Data Types

Validate Table Column Data

Description
This customization demonstrates how to customize the Data Access tier of your application so that the entire application uses a custom class to validate, format, and parse all values for a specific column in a Table. The example uses very simple validation and formatting logic, but you can modify the sample code to implement your own business rules.
Variables
Table Name
Select a database table having currency column
Validate Column
Select the name of the currency column to validate
Applies to
DataAccessClass class
Code
 
// Customization for ${Table Name}Access class.
// A custom column class that only accepts MyCustom Currency values.
[Serializable()] 
public class MyCustomCurrencyColumn : BaseClasses.Data.CurrencyColumn 
{ 
    public MyCustomCurrencyColumn(BaseClasses.Data.CurrencyColumn col)
    : base(col.Number, col.InternalName, col.Name, col.TableDefinition, null, null, null) 
    { 
    this.IsIdentity = col.IsIdentity; 
    this.DbDataType = col.DbDataType; 
    this.DefaultValue = col.DefaultValue; 
    this.DatabaseDefaultValue = col.DatabaseDefaultValue; 
    this.DisplayFormat = col.DisplayFormat; 
    this.DbFormat = col.DbFormat; 
    this.IsRequired = col.IsRequired; 
    this.IsIndexed = col.IsIndexed; 
    this.IsUnique = col.IsUnique; 
    this.IsValuesReadOnly = col.IsValuesReadOnly; 
    this.IsValuesComputed = col.IsValuesComputed; 
    } 

    // Overridden to return false for non-valid values.
    public override bool IsValidValue(BaseClasses.Data.ColumnValue value) 
    { 
        value = this.ConvertToNativeFormat(value); 
        if (!(base.IsValidValue(value))) 
        { 
            return false; 
        } 
        else if (value == null || value.IsNull) 
        { 
            return true; 
        } 
        else 
        { 
            // Add custom data validation here as shown below.
            // Return false if value is negative.
            // if ((System.Convert.ToDouble(value.Value) <= 0)) 
            // { 
            //     return false; 
            // }
        } 
        return true; 
    } 

    // Overridden to return empty string if not valid value.
    public override string ToDisplayString(ColumnValue value, string format) 
    { 
        if ((IsValidValue(value))) 
        { 
            return base.ToDisplayString(value, format); 
        } 
        else 
        { 
            return ""; 
        } 
    } 
} 

// Overridden to customize this object's TableDefinition's ColumnList.
protected override void Initialize() 
{ 
    base.Initialize(); 

    // Construct a custom column instance using the current ${Validate Column}.
    MyCustomCurrencyColumn newCol = new MyCustomCurrencyColumn(this.${Validate Column}Column); 

    // Replace the ${Validate Column} with the custom column instance.
    int i; 
    i = this.TableDefinition.ColumnList.IndexOf(this.${Validate Column}Column); 
    this.TableDefinition.ColumnList.RemoveAt(i);
    this.TableDefinition.ColumnList.Insert(i,newCol);
}
     

Terms of Service Privacy Statement